﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.Commons.DataFilters;
namespace SD.DataAccess
{
    /// <summary>
    /// Address class which duels with database
    /// </summary>
    public partial class Address
    {
        /// <summary>
        /// get addresses from database
        /// </summary>
        /// <param name="filter">filter with contains street, town, address</param>
        /// <returns>address</returns>
        public static IQueryable<Address> GetAddresses( AddressFilter filter)
        {
            //Open connection
            ServiceDBDataContext db = new ServiceDBDataContext();

            //Get record from it
            System.Linq.IQueryable<Address> addresses = db.Addresses;

            //begin filter
            bool blnPostCodeEmpty = string.IsNullOrEmpty(filter.PostCode);
            bool blnAddressEmpty = string.IsNullOrEmpty(filter.AddressLine);
            bool blnTownEmpty = string.IsNullOrEmpty(filter.Town);
            bool blnStreetEmpty = string.IsNullOrEmpty(filter.Street);

            if (!blnPostCodeEmpty&&!blnStreetEmpty&&!blnTownEmpty )
                addresses = addresses.Where(a => a.Postcode.Contains(filter.PostCode) &&  a.Town.TownName.Contains(filter.Town) && a.AddressLine.Contains(filter.Street));
            
            else if (blnPostCodeEmpty && !blnStreetEmpty && !blnTownEmpty)
                addresses = addresses.Where(a => a.Town.TownName.Contains(filter.Town) && a.AddressLine.Contains(filter.Street));

            else if (!blnPostCodeEmpty && blnStreetEmpty && !blnTownEmpty)
                addresses = addresses.Where(a => a.Postcode.Contains(filter.PostCode) && a.Town.TownName.Contains(filter.Town));

            else if (!blnPostCodeEmpty && !blnStreetEmpty && blnTownEmpty)
                addresses = addresses.Where(a => a.Postcode.Contains(filter.PostCode) && a.AddressLine.Contains(filter.Street));

            else if (blnPostCodeEmpty && blnStreetEmpty && !blnTownEmpty)
                addresses = addresses.Where(a => a.Town.TownName.Contains(filter.Town));

            else if (blnPostCodeEmpty && !blnStreetEmpty && blnTownEmpty)
                addresses = addresses.Where(a =>   a.AddressLine.Contains(filter.Street));

            else if (!blnPostCodeEmpty && blnStreetEmpty && blnTownEmpty)
                addresses = addresses.Where(a => a.Postcode.Contains(filter.PostCode));
         
            return addresses;    

        }

        /// <summary>
        /// method to get an address record by id from database
        /// </summary>
        /// <param name="id">address id</param>
        /// <returns>Address entity</returns>
        public static Address GetAddressById(int id)
        {
            //Open database
            ServiceDBDataContext db = new ServiceDBDataContext();

            if (db.Addresses.Any(a => a.AddressId == id))
                return db.Addresses.Single(a => a.AddressId == id);
            return null;

        }
    }
}
